//
//  PerfectMessageViewController.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/4.
//

#import "PerfectMessageViewController.h"
#import "UserRequest.h"
#import "ChooseSexView.h"
#import "ChooseAgeView.h"
#import "WeightViewController.h"
#import "AppDelegate.h"
#import "BaseNavigationController.h"
#import "BaseTabBarController.h"

@interface PerfectMessageViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *nameF;
@property (nonatomic, strong) UITextField *codeF;
@property (nonatomic, strong) UIButton *codeBtn;
@property (nonatomic, strong) UITextField *ageF;
@property (nonatomic, strong) UIButton *ageBtn;
@property (nonatomic, strong) UITextField *sexF;
@property (nonatomic, strong) UIButton *sexBtn;
@property (nonatomic, strong) UITextField *weightF;
@property (nonatomic, strong) UIButton *registerBtn;

@end

@implementation PerfectMessageViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];

}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"完善注册";
    [self configUI];
    // Do any additional setup after loading the view.
}

- (void)ageBtnClicked:(UIButton *)sender {
    [ChooseAgeView chooseAgeWith:self Complection:^(NSString *age) {
        self.ageF.text = age;
        [NSObject.getKeyWindowRootViewController gq_removeBgView];
    }];
}

- (void)sexBtnClicked:(UIButton *)sender {
    [ChooseSexView chooseSexWith:self Complection:^(int sex) {
        self.sexF.text = sex == 0 ? @"女" : @"男";
        [NSObject.getKeyWindowRootViewController gq_removeBgView];
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)configUI {
    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.image = [UIImage imageNamed:@"路径"];
    [self.view addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(18 + self.navigationHeight);
        make.leading.equalTo(self.view.mas_leading).offset(24);
        make.trailing.equalTo(self.view.mas_trailing).offset(-24);
        make.height.offset(132*kHEIGHT_RATIO);
    }];
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = @"你好,\n欢迎来到骑行!";
    titleL.textColor = rgb(48, 48, 48);
    titleL.numberOfLines = 0;
    titleL.font = [UIFont systemFontOfSize:28];
    [self.view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(topImageView.mas_top).offset(12);
    }];
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = rgb(10, 211, 159);
    [self.view addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleL.mas_left);
        make.top.equalTo(titleL.mas_bottom).offset(5);
        make.width.offset(38);
        make.height.offset(4);
    }];
    
    UILabel *nameL = [[UILabel alloc] init];
    nameL.text = @"姓名*";
    nameL.textColor = rgb(166, 166, 166);
    nameL.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(topImageView.mas_bottom).offset(5);
        make.height.offset(50);
        make.width.offset(60);
    }];
    
    UITextField *nameF = [[UITextField alloc] init];
    nameF.placeholder = @"请输入姓名";
    nameF.keyboardType = UIKeyboardTypeDefault;
    nameF.textColor = rgbHex(0xA6A6A6,1.0);
    nameF.delegate = self;
    nameF.clearButtonMode = UITextFieldViewModeWhileEditing;
    nameF.borderStyle = UITextBorderStyleNone;
    [self.view addSubview:nameF];
    self.nameF = nameF;
    [nameF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameL.mas_right).offset(22);
        make.top.equalTo(topImageView.mas_bottom).offset(5);
        make.right.equalTo(self.view.mas_right).offset(-28);
        make.height.offset(50);
    }];
    
    UIView *nameV = [[UIView alloc] init];
    nameV.backgroundColor = rgb(238, 238, 238);
    [self.view addSubview:nameV];
    [nameV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(31);
        make.top.equalTo(nameF.mas_bottom).offset(0);
        make.right.equalTo(self.view.mas_right).offset(-31);
        make.height.offset(1);
    }];
    
    UILabel *ageL = [[UILabel alloc] init];
    ageL.text = @"年龄*";
    ageL.textColor = rgb(166, 166, 166);
    ageL.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:ageL];
    [ageL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(nameV.mas_bottom).offset(17);
        make.height.offset(50);
        make.width.offset(60);
    }];
    
    UITextField *ageF = [[UITextField alloc] init];
    ageF.placeholder = @"请选择年龄";
    ageF.borderStyle = UITextBorderStyleNone;
    ageF.textColor = rgbHex(0xA6A6A6,1.0);
    ageF.clearButtonMode = UITextFieldViewModeWhileEditing;
    ageF.keyboardType = UIKeyboardTypeDefault;
    ageF.delegate = self;
    ageF.userInteractionEnabled = NO;
    [self.view addSubview:ageF];
    self.ageF = ageF;
    [ageF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ageL.mas_right).offset(22);
        make.top.equalTo(nameV.mas_bottom).offset(17);
        make.right.equalTo(self.view.mas_right).offset(-104);
        make.height.offset(50);
    }];
    
    UIView *ageView = [[UIView alloc] init];
    ageView.backgroundColor = rgb(238, 238, 238);
    [self.view addSubview:ageView];
    [ageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(28);
        make.top.equalTo(ageF.mas_bottom).offset(0);
        make.right.equalTo(self.view.mas_right).offset(-28);
        make.height.offset(1);
    }];
    
    UIButton *ageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [ageBtn setImage:[UIImage imageNamed:@"image_open"] forState:UIControlStateNormal];
    [ageBtn addTarget:self action:@selector(ageBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:ageBtn];
    self.ageBtn = ageBtn;
    [ageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-26);
        make.centerY.equalTo(ageF.mas_centerY);
        make.width.offset(46);
        make.height.offset(46);
    }];
    
    UILabel *sexL = [[UILabel alloc] init];
    sexL.text = @"性别*";
    sexL.textColor = rgb(166, 166, 166);
    sexL.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:sexL];
    [sexL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(ageView.mas_bottom).offset(17);
        make.height.offset(50);
        make.width.offset(60);
    }];
    
    UITextField *sexF = [[UITextField alloc] init];
    sexF.placeholder = @"请选择性别";
    sexF.textColor = rgbHex(0xA6A6A6,1.0);
    sexF.borderStyle = UITextBorderStyleNone;
    sexF.clearButtonMode = UITextFieldViewModeWhileEditing;
    sexF.keyboardType = UIKeyboardTypeDefault;
    sexF.delegate = self;
    sexF.userInteractionEnabled = NO;
    [self.view addSubview:sexF];
    self.sexF = sexF;
    [sexF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sexL.mas_right).offset(22);
        make.top.equalTo(ageF.mas_bottom).offset(17);
        make.right.equalTo(self.view.mas_right).offset(-104);
        make.height.offset(50);
    }];
    
    UIView *sexView = [[UIView alloc] init];
    sexView.backgroundColor = rgb(238, 238, 238);
    [self.view addSubview:sexView];
    [sexView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(28);
        make.top.equalTo(sexF.mas_bottom).offset(0);
        make.right.equalTo(self.view.mas_right).offset(-28);
        make.height.offset(1);
    }];
    
    UILabel *weightL = [[UILabel alloc] init];
    weightL.text = @"体重*";
    weightL.textColor = rgb(166, 166, 166);
    weightL.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:weightL];
    [weightL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImageView.mas_left).offset(3);
        make.top.equalTo(sexView.mas_bottom).offset(17);
        make.height.offset(50);
        make.width.offset(60);
    }];
    
    UITextField *weightF = [[UITextField alloc] init];
    weightF.placeholder = @"请输入体重(单位:kg)";
    weightF.textColor = rgbHex(0xA6A6A6,1.0);
    weightF.borderStyle = UITextBorderStyleNone;
    weightF.userInteractionEnabled = YES;
    weightF.clearButtonMode = UITextFieldViewModeWhileEditing;
    weightF.delegate = self;
    weightF.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:weightF];
    self.weightF = weightF;
    [weightF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weightL.mas_right).offset(22);
        make.top.equalTo(sexView.mas_bottom).offset(17);
        make.right.equalTo(self.view.mas_right).offset(-104);
        make.height.offset(50);
    }];
    
    UIButton *weightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [weightBtn addTarget:self action:@selector(setWeight) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:weightBtn];
    [weightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weightL.mas_right).offset(22);
        make.top.equalTo(sexView.mas_bottom).offset(17);
        make.right.equalTo(self.view.mas_right).offset(-104);
        make.height.offset(50);
    }];
    UIView *weightView = [[UIView alloc] init];
    weightView.backgroundColor = rgb(238, 238, 238);
    [self.view addSubview:weightView];
    [weightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(28);
        make.top.equalTo(weightF.mas_bottom).offset(0);
        make.right.equalTo(self.view.mas_right).offset(-28);
        make.height.offset(1);
    }];
    
    UIButton *sexBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sexBtn setImage:[UIImage imageNamed:@"image_open"] forState:UIControlStateNormal];
    
    [sexBtn addTarget:self action:@selector(sexBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sexBtn];
    self.sexBtn = sexBtn;
    [sexBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-26);
        make.centerY.equalTo(sexF.mas_centerY);
        make.width.offset(46);
        make.height.offset(46);
    }];
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerBtn setTitle:@"提交" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn setBackgroundImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:24];
    [registerBtn addTarget:self action:@selector(perfectMessage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
    self.registerBtn = registerBtn;
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(26);
        make.right.equalTo(self.view.mas_right).offset(-26);
        make.top.equalTo(weightView.mas_bottom).offset(20);
        make.height.offset(62);
    }];
    
}

- (void)setWeight {
    WeightViewController *vc = [[WeightViewController alloc] init];
    vc.block = ^(NSString * _Nonnull weight) {
        self.weightF.text = weight;
    };
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)perfectMessage {
    
    if (IsStrEmpty(self.nameF.text)) {
        [HUDHelper showCustomAlertMessage:@"请输入姓名"];
        return;
    }
    if (IsStrEmpty(self.ageF.text)) {
        [HUDHelper showCustomAlertMessage:@"请选择年龄"];
        return;
    }
    if (IsStrEmpty(self.sexF.text)) {
        [HUDHelper showCustomAlertMessage:@"请选择性别"];
        return;
    }
    int sex = -1;
    if ([self.sexF.text isEqualToString:@"男"]) {
        sex = 1;
    } else if ([self.sexF.text isEqualToString:@"女"]){
        sex = 0;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [UserRequest completeUserFileWithName:self.nameF.text age:self.ageF.text.intValue weight:self.weightF.text gender:[NSString stringWithFormat:@"%d",sex] result:^(BOOL ret) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (ret) {
            appDelegate.window.rootViewController = [[BaseNavigationController alloc] initWithRootViewController:[[BaseTabBarController alloc] init]];
            [appDelegate.window makeKeyAndVisible];
        }
    }];
}

//- (void)textFieldDidEndEditing:(UITextField *)textField {

//    if (!IsStrEmpty(self.nameF.text) && !IsStrEmpty(self.sexF.text) && !IsStrEmpty(self.ageF.text) && !IsStrEmpty(self.weightF.text)) {
//        [self.registerBtn setBackgroundImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
//    } else {
//        [self.registerBtn setBackgroundImage:[UIImage imageNamed:@"btn_shense"] forState:UIControlStateNormal];
//    }
//}
@end
